/***********************************************************************
    filename:   hgeFontManager.cpp
    created:    Sun Jul 19 2009
    author:     Paul D Turner <paul@cegui.org.uk>
*************************************************************************/
#include "hgeFontManager.h"
#include "hgeExceptions.h"
#include "hgeLogger.h"
#include "hgeGuiSystem.h"

//----------------------------------------------------------------------------//
// singleton instance pointer
template<> FontManager* Singleton<FontManager>::ms_Singleton = 0;

//----------------------------------------------------------------------------//
FontManager::FontManager()
{
    char addr_buff[32];
    sprintf(addr_buff, "(%p)", static_cast<void*>(this));
    hgeLogger::getSingleton().logEvent(
        "FontManager singleton created. " + String(addr_buff));
}

//----------------------------------------------------------------------------//
FontManager::~FontManager()
{
    hgeLogger::getSingleton().logEvent(
        "---- Begining cleanup of Font system ----");

	while (!d_fonts.empty())
		destroyFont(d_fonts.begin());

    char addr_buff[32];
    sprintf(addr_buff, "(%p)", static_cast<void*>(this));
    hgeLogger::getSingleton().logEvent(
        "FontManager singleton destroyed. " + String(addr_buff));
}

//----------------------------------------------------------------------------//
hgeFont& FontManager::createFont(const String& font_name, const String& font_filename,
                                      const float point_size,
                                      const bool anti_aliased)
{
    hgeLogger::getSingleton().logEvent("Attempting to create FreeType font '" +
        font_name + "' using font file '" + font_filename + "'.");
	
	FontRegistry::const_iterator i(d_fonts.find(font_name));

	if (i != d_fonts.end())
		return getFont(font_name);

    // create new object ahead of time
    hgeFont* object = new hgeFont(font_name, point_size, anti_aliased, font_filename);

	// if this was the first font created, set it as the default font
	if (d_fonts.size() == 1)
		System::getSingleton().setDefaultFont(object);
	
	d_fonts[font_name] = object;

    // return appropriate object instance (deleting any not required)
    return *object;
}

hgeFont& FontManager::getFont(const String& object_name) const
{
	FontRegistry::const_iterator i(d_fonts.find(object_name));

	if (i == d_fonts.end())
		throw UnknownObjectException("FontManager::getFont: "
		"No object of named '" + object_name + "' is present in the collection.");

	return *i->second;
}

void FontManager::destroyFont(FontRegistry::iterator ob)
{
	char addr_buff[32];
	sprintf(addr_buff, "(%p)", static_cast<void*>(ob->second));
	hgeLogger::getSingleton().logEvent("Font named '" + ob->first + "' has been destroyed. " +
		addr_buff, Informative);

	delete ob->second;
	d_fonts.erase(ob);
}

//----------------------------------------------------------------------------//
void FontManager::notifyDisplaySizeChanged(const Size& size)
{
    // notify all attached Font objects of the change in resolution
    FontRegistry::iterator pos = d_fonts.begin(), end = d_fonts.end();

    /*for (; pos != end; ++pos)
        pos->second->notifyDisplaySizeChanged(size);*/
}

//----------------------------------------------------------------------------//
FontManager::FontIterator FontManager::getFontIterator(void) const
{
    return FontIterator(d_fonts.begin(), d_fonts.end());
}